import { TreeNode } from "@/基本数据结构/二叉树";

export function buildTree(inorder: number[], postorder: number[]): TreeNode | null {
    if (!inorder.length || inorder.length !== postorder.length) return null
    const tempIn = JSON.parse(JSON.stringify(inorder))
    const temppost = JSON.parse(JSON.stringify(postorder))
    if (tempIn.sort().join() !== temppost.sort().join()) return null
    const root = new TreeNode(postorder[postorder.length - 1])
    const index = inorder.findIndex((val) => val === postorder[postorder.length - 1])

    root.left = buildTree(inorder.slice(0, index), postorder.slice(0, index))
    root.right = buildTree(inorder.slice(index + 1, inorder.length), postorder.slice(index, postorder.length - 1))

    return root
};